var enumStyle = {"LIST_SIMPLE":0,"LIST_INDEX":1,"LIST_IMAGE":2,"LIST_DETAIL":3,"LIST_SCORE":4,"GRID_IMAGE":5,"LIST_GOLD":6};

var enumData = {"Name":0,"SUBNAME":1,"IMAGE":2,"IDS":3};

var enumJson = {"images":"getJsonImage","games":"getJsonGame","tygia":"getJsonCurrency","audio":"getJsonAudio",
				"videos":"getJsonVideo","films":"getJsonFilm","weather":"getJsonWeather"};

var domain = "http://210.86.231.120:7001/MOBILE/";
var serverDownload = "http://210.86.231.120:7001/MOBILE/download?";
var serverJson = "http://210.86.231.120:7001/MOBILE/ServiceJsonService";
var serverReftion = "http://JsonUtils/";
var g_userName = "HungNM"; //HungNM and vimic
	
var enumMonitor ={ "MIN_SIZE_ICON":200, "MAX_SIZE_ICON":300};	
var g_nSizeIcon = 0;
var g_nInLine = 0;

var g_nHeight = 0;
var g_nWidth = 0;

var ArrExtendId = new Array();
/*Device phone gap*/

//document.addEventListener("deviceready", onDeviceReady, false);
/*
function onDeviceReady() 
{	
	if(checkConnection())
	{
		initSizeIcon();	
		getJsonData("getJsonCategoryExtend",g_userName,-1,displayHome); //displayHome is call back function
	}
	
}
*/
$("#home").live('pageinit', function()
 {
	initSizeIcon();		
	getJsonData("getJsonCategoryExtend",g_userName,-1,displayHome); //displayHome is call back function
	ArrExtendId.push(-1);
 });
			
$(document).bind( "pagebeforechange", function( e, data ) {	
	if ( typeof data.toPage === "string" ) {

		// We are being asked to load a page by URL, but we only
		// want to handle URLs that request the data for a specific
		// category.
		var url = $.mobile.path.parseUrl( data.toPage ),
			re = /^#category-item/;
		alert("changepage");
		if ( url.hash.search(re) !== -1 ) {

			// We're being asked to display the items for a specific category.
			// Call our internal method that builds the content for the category
			// on the fly based on our in-memory category data structure.
			processChangePage( url, data.options );

			// Make sure to tell changePage() we've handled this call so it doesn't
			// have to do anything.
			e.preventDefault();
		}
	}
});

function processChangePage(urlObj,options)
{
	// Handle URLs that requests  page	
	
	var idLink = urlObj.hash.replace( /.*id=/, "" );
	var nCurrentLevel =  ArrExtendId.length;
	var nPos = ArrExtendId.indexOf(idLink);
	if(nPos == -1) //not found
	{
		alert('push' + idLink);
		ArrExtendId.push(idLink);
	}
	else
	{
		alert(idLink + ' at: '+nPos)
		ArrExtendId.slice(0,nPos+1);
	}
	var realLevel = ArrExtendId.length;
	
	alert(realLevel);
	var	pageSelector = urlObj.hash.replace( /\?.*$/, "" );
	var sidPage = 'id' + idLink; //ex: #id12789	->	id12789		
	//Get moreInfo			
	var spanID = "#span-category" + idLink;	
	//$("#write_level").innerHTML = spanID;
	var info = $(spanID).text();	//issue same id , so get idPage fail
	var  arrInfo = info.split(",");  //name - extendCommand	
	//Create new page	
	var bCreatePage = true;	
	//alert(sidPage);
	if(sidPage =="viewImage")
	{
		bCreatePage = false;
		var nPos = arrInfo[0];
		
		createSlideShow('#id'+arrInfo[1],nPos);
	}	
	//if(bCreatePage && $('#'+sidPage).length ==0) // check exist
	//	createPage(sidPage,arrInfo[0]);	
	if(arrInfo[1] == -1)//extendCommand = -1 => parrentId = extendId
	{				
		//var idParrent = sidPage.replace("id","");
		getJsonData("getJsonCategoryExtend",g_userName,idLink,diplayCategory);
	}
	else if(arrInfo[1] == "images")	
	{			
		getListItems("getJsonImage",g_userName,idLink,showgridImage); //test
	}
	else if(arrInfo[1] == "audio")
	{	
		getListItems("getJsonAudio",g_userName,arrInfo[0],showListAudio); // test
	}	
	var $page = $("#category-items");
	
	$page.page();
	
	options.dataUrl = urlObj.href;
	$.mobile.changePage( $page, options );
}
/*End init*/
function initSizeIcon()
{	
	if(window.innerHeight !==undefined)
	{
		g_nWidth = window.innerWidth;
		g_nHeight = window.innerHeight;
	}	
	else	// IE
	{ 
		var temp= (document.body.clientWidth)? document.body: document.documentElement;
		g_nWidth = temp.clientWidth;
		g_nHeight = temp.clientHeight;
	}
	
	var nMin = parseInt(g_nWidth/enumMonitor.MAX_SIZE_ICON);
	var nMax = parseInt(g_nWidth/enumMonitor.MIN_SIZE_ICON);
	
	g_nInLine = parseInt((nMin + nMax)/2);
	
	var sizeCell = g_nWidth/g_nInLine;
	
	//Using 10% for border
	g_nSizeIcon = parseInt(sizeCell*0.8); 
}

//Utils
function getParretID(id)
{
	var sId = '#' + id;
	var parentId = $(sid).parent().id;
}

function getIdOfpage(url)
{
	var arr = url.split("#");
	var length = arr.length;
	if(length == 1)
		return "home";
	else
		return arr[length -1];
}

function calculateSizeControl(numItem)
{   
	 
	var dimmension;	
	if(window.innerHeight !==undefined)
		dimmension= [window.innerWidth,window.innerHeight]; // most browsers
	else
	{ // IE varieties
		var temp= (document.body.clientWidth)? document.body: document.documentElement;
		dimmension= [temp.clientWidth,temp.clientHeight];	
	}
	
	var width = dimmension[0];
	var Height = dimmension[1];
	
	
	var num = new Number(width);		
	var nSizeBtn = num/(numItem+1);
	var align = num/6;
	var arrInfo=new Array(); 		 // regular array (add an optional integer
	arrInfo[0]= nSizeBtn+ 'px';       // size button
	arrInfo[1]= align + 'px';		
	return arrInfo;
}
//End utils

	
function changDisplay(idPage,header,content,footer,typeRefresh)
{
	
	var $page = $( idPage );
	if($page)
	{
			// Get the header for the page.
		var $header = $page.children( ":jqmData(role=header)" );
			// Get the content area element for the page.
		var	$content = $page.children( ":jqmData(role=content)" );
			// Get the footer area element for the page.
		var	$footer = $page.children( ":jqmData(role=footer)" );
		
		// Find the h1 element in our header and inject the name of
		$header.find( "h1" ).html(header);
		// Inject the category items markup into the content element.
		$content.html( content );
		//Add footer
		$footer.html(footer);
		$page.page();
		// Enhance the control just injected.
		if(typeRefresh == enumStyle.LIST_SIMPLE)
			$content.find( ":jqmData(role=listview)" ).listview();
		// we just loaded.
		//options.dataUrl = urlObj.href;

		// Now call changePage() and tell it to switch to
		// the page we just modified.
		//$.mobile.changePage( $page, options );
	}
}
//End bindingJsM

//GridView


function createGridIcon(idPage,arrLink,arrName,arrICon,arrCommand,numItem,bEmpty)
{
	
	var classBlock = new Array("ui-block-a","ui-block-b","ui-block-c","ui-block-d","ui-block-e");
	var uiGrid = new Array ("ui-grid-a","ui-grid-b","ui-grid-c");
	var $page = $(idPage);
	if($page.length == 0)
		alert("not exist page:" + idPage);
	var	$content = $page.children( ":jqmData(role=content)");
	if($content && $content.children().length ==0)
	{
		var arrInfoMonitor = calculateSizeControl(numItem);
		var nLength = arrLink.length;
		var nCount = 0;			
		while(nCount!= nLength)
		{	
			//append fieldset				
			var sField = '<fieldset class='+ uiGrid[numItem-2] +'>';				
			var x = 0;
			var items = ''; 
			var eachLoop = numItem;
			if((nLength - nCount) < numItem)
				eachLoop = nLength - nCount;
			
			for (x =0 ; x < eachLoop; x++)
			{	
				var idExtend = arrLink[nCount].replace('#category-items&id=','');			
				var moreInfo = arrName[nCount] +','+	arrCommand[nCount];//name,extendCommand    												
				var item = 
				'<div class="'+classBlock[x]+ '">' +
					'<a href="'+arrLink[nCount]+'" class = "grid-link">'+
						'<div >'+
							'<img  src="'+arrICon[nCount] +'"class="center" />'+
							' <span id ="'+'span-category'+idExtend+'"style="display: none;">'+moreInfo+'</span>'+								
						'</div>'+
						'<div>'+
							'<p class = "align-text">'+arrName[nCount] +'</p>'+
						'</div>'+
					'</a>'+
				'</div> ';
				items = items +item;					
				nCount++;
			}	
			var sEndField = '</fieldset>';	
			$content.append(sField + items +sEndField);
		}				
			
	}
}

//listView
function populateList(idPage,arrLink,arrName,arrCommand,bRefresh) 
{    		
	var $page = $(idPage);
	if($page)    
	{	
		var	$content = $page.children( ":jqmData(role=content)");
		if($content && $content.children().length ==0)
		{
			 var $content = $page.children( ":jqmData(role=content)" );	 	     
			  var x;		 
			  var sItems = '<ul  data-filter="true" data-role="listview"  data-theme="b">';
			  for(x in arrName)
			  {
			    var moreInfo = arrName[x] +','+	arrCommand[x];//name,extendCommand  
				var item = '<li>'+
				'<a href="' + arrLink[x] +'">' +arrName[x] + '</a>'+
				'<span id ="'+'span-'+arrLink[x].replace("#","")+'"style="display: none;">'+moreInfo+
				'</span>'+
				'</li>';
				
				sItems = sItems + item;
			  }
			  sItems = sItems +'</ul>';
			  $content.html(sItems);		  
			  if(bRefresh)
				$content.find( ":jqmData(role=listview)").listview();//must be called if you want the new list elements properly styled
		}			
	}
 };
 
 function populateListWithIndex(idPage,arrLink,arrName,bRefresh) 
{    		
	var $page = $(idPage);
	if($page)    
	{	
		var	$content = $page.children( ":jqmData(role=content)");
		if($content && $content.children().length ==0)
		{
			 var $content = $page.children( ":jqmData(role=content)" );	 	     
			  var x;		 
			  var sItems = '<ul  data-filter="true" data-role="listview"  data-theme="c">';
			  for(x in arrName)
			  {
				var item = "<li><a href='" + arrLink[x] +"'>" +'<span style="color:darkolivegreen;font-weight:bold">'+x+'.  ' +'</span>' +arrName[x] + "</a></li>";
				sItems = sItems + item;
			  }
			  sItems = sItems +'</ul>';
			  $content.html(sItems);		  
			  if(bRefresh)
				$content.find( ":jqmData(role=listview)").listview();//must be called if you want the new list elements properly styled
		}			
	}
 };


function CreateListShortView(id,arrLink,arrImg,arrShort,arrDate) // include game, lifestyle
{
	var items = '<ul data-role="listview" data-filter="false">';
	var i;
	for (i in arrLink)
	{
		var item = 	'<li>'+
				'<a href="'+ arrLink[i] +'">'+
				'<img style=" margin-left: 5px;  margin-top: 5px ; " src="'+arrImg[i]+'"/> '+
				'<h4> '+arrShort[i]+ '</h4>'+
				'<p> '+arrDate[i]+'</p>'+					
				'</a>'+
			'</li>';
		items = items + item;
	}
	items = items + '<ul>';
	$(id).append(items);
}

function createListGold(id,arrCenter,arrCode,arrDataNew,arrDataOld)
{
	var items = '<ul data-role="listview" data-filter="false">';
	var i;
	for (i in arrCenter)
	{
		var item = '<li>' +
				   '<div class="head">'+arrCenter[i]+'</div>' +
				   '<div data-role="navbar" >'+
					'<ul>'+
						'<li><p style="border-right:2px solid green">'+arrCode[i]+'</p></li>'+
						'<li><p style="border-right:2px solid green">'+arrDataNew[i]+'</p></li>'+
						'<li><p>'+arrDataOld[i]+'</p></li>'+
					'</ul>'+
				'</div>'+
				'</li>';
		items = items + item;		
		
	}
	items = items + '</ul>';
	$(id).append(items);
}
function createListViewScore(id,date,arrNameLeft,arrNameRight,arrImgLeft,arrImgRight) 
{
	var items = '<ul data-role="listview" data-filter="false" data-theme="b">';
	
	var i;
	for (i in arrNameLeft)
	{
		var item = '<li>'+
				'<div class="head_score">'+ date + '</div>'+						
				'<center>'+
					'<table style="margin-left: 10">'+
						'<tr>'+
							'<td style="text-align:left; width: 20">'+arrNameLeft[i]+'</td>'+
							'<td><img src="'+arrImgLeft[i]+'"/></td>'+
							'<td> -::- </td>'+
							'<td><img src="'+arrImgRight[i]+'"/></td>'+
							'<td style="text-align:right; width: 20">'+arrNameLeft[i]+'</td>'+			 		
						'</tr>'+
					'</table>'+
				'</center>'+
				'<p style="text-align: center;  font-size: 10px">Ngay17/06/2013</p>'+
			'</li>';
			items = items + item;
	}
	items = items + '</ul>';
	$(id).append(items);
}
//End listView

//vimic


	function createPage(id,title)
	{		
		var page = '<div data-role="page" id="' + id +'">'+
		'<div data-role="header" data-theme="b">'+
			'<h1>'+title +'</h1>'+'</div>'+
		'<div data-role="content">'+
		'</div>'+
		'<div data-role="footer" data-theme="d"></div>'+
	'</div>';		
		$('body').append(page);
		$('#'+id).page();

	}
	
	function displayHome(jSondata)
	{			
		var arrLink = new Array();
		var arrName = new Array();
		var arrImage = new Array();
		var arrCommand = new Array();
		var ncount = 0;
		while(1)
		{
			if(jSondata[ncount] == null)
				break;
			arrName[ncount] = jSondata[ncount].extendName;
			arrImage[ncount] = serverDownload +jSondata[ncount].extendIcon + '&w='+g_nSizeIcon;
			arrLink[ncount] = '#category-items&id='+jSondata[ncount].extendId;
			arrCommand[ncount] = jSondata[ncount].extendCommand;		
			ncount ++;			
		}			
		createGridIcon("#home",arrLink,arrName,arrImage,arrCommand,g_nInLine,false,false);
	}
	function diplayCategory(jSondata)
	{
		//function createGridImage(id,arrLink,arrName,arrImage,numItem,bRefresh)	
		var arrLink = new Array();
		var arrName = new Array();
		var arrImage = new Array();
		var arrExtends = new Array();
		var ncount = 0;
		var bHasImg = false;
		while(1)
		{
			if(jSondata[ncount] == null)
				break;
			arrName[ncount] = jSondata[ncount].extendName;
			arrImage[ncount] = serverDownload +jSondata[ncount].extendIcon + '&w=' + g_nSizeIcon;
			if(bHasImg == false && jSondata[ncount].extendIcon)
				bHasImg = true;
			arrLink[ncount] = '#category-items&id='+jSondata[ncount].extendId;
			arrExtends[ncount] = jSondata[ncount].extendCommand;
			ncount ++;
		}
		if(ncount > 0)
		{		
		
		  var parentId = jSondata[0].parentId;		 
		  var idpage = "#id" + parentId;
		  if(bHasImg)
			createGridIcon('#category-items',arrLink,arrName,arrImage,arrExtends,g_nInLine,false,true);
		  else
			populateList(idpage,arrLink,arrName,arrExtends,true);
		}
		 else
		 {
			alert("subcategory is empty");
		 }
	}
	function viewHTML(content)
	{
	}
	
	var imgs = null;	
	var imageIndex = 0;
	function slideImage(bIncrease)
	{
		$("#MainImg").fadeOut("slow", function() {
			
			var temp = imgs[imageIndex].src.split("&");
			var realImg = temp[0]+"&w=" +g_nWidth;	
	        $(this).attr("src", realImg).fadeIn("slow");
	        if(bIncrease)
        	{
	        	++imageIndex;
	        	if(imageIndex>=imgs.length)
		            imageIndex=0;
        	}	      
	        else
        	{
	        	--imageIndex;
	        	if(imageIndex<=0)
		            imageIndex=imgs.length-1;
        	}
	        	
	        
	    });
	}
	
	$('#MainImg').live('swiperight', function() { 
		
		 slideImage(true);
		return false;
    });
	
	$('#MainImg').live('swipeleft', function() { 
		
		 slideImage(false);
		return false;
   });
	
	function createSlideShow(idPage,nIndex)
	{
		//get link url of image				
		if($(idPage))
		{			
			imgs = $(idPage).find('img');		
			var temp = imgs[nIndex].src.split("&");			
			var realImg = temp[0]+"&w=" +g_nWidth;			
			$("#MainImg").attr("src", realImg);
			imageIndex = nIndex;
		}

		
	}
	function createGridImage(idPage,arrLink,arrName,arrImage,arrCommand,numItem,bRefresh,bEmpty)
	{
		var classBlock = new Array("ui-block-a","ui-block-b","ui-block-c","ui-block-d","ui-block-e");
		var uiGrid = new Array ("ui-grid-a","ui-grid-b","ui-grid-c");
		var $page = $(idPage);
		$("#write_level").innerHTML =  'createGridImage id :' + idPage;
		if($page.length == 0)
			alert("not exist page:" + idPage);
		var	$content = $page.children( ":jqmData(role=content)");		
		if($content)
		{		
			var arrInfoMonitor = calculateSizeControl(numItem);
			var nLength = arrLink.length;
			var nCount = 0;			
			while(nCount!= nLength)
			{	
				//append fieldset				
				var sField = '<fieldset class='+ uiGrid[numItem-2] +'>';				
				var x = 0;
				var items = ''; 
				var eachLoop = numItem;
				if((nLength - nCount) < numItem)
					eachLoop = nLength - nCount;
				
				for (x =0 ; x < eachLoop; x++)
				{	
					var moreInfo = x +','+	arrCommand[nCount];						
					var item = 
					'<div class="'+classBlock[x]+ '">' +
						'<a href="'+arrLink[nCount]+'" class = "grid-link">'+
							'<div >'+
								'<img  src="'+arrImage[nCount] +'" class="center" />'+
								' <span id ="'+'span-'+arrLink[nCount].replace("#","")+'"style="display: none;">'+moreInfo+'</span>'+								
							'</div>'+
							'<div>'+
								'<p class = "align-text">'+arrName[nCount] +'</p>'+
							'</div>'+
						'</a>'+
					'</div> ';
					items = items +item;					
					nCount++;
				}	
				var sEndField = '</fieldset>';	
				
				$content.html(sField + items +sEndField);
			}				
				
		}		
	}
	
	function showgridImage(jSondata)
	{
	  
	  var arrLink = new Array();
	  var arrName = new Array();
	  var arrImage = new Array();
	  var arrExtends = new Array();
	  var ncount = 0;
	  while(1)
		{
			if(jSondata[ncount] == null)
				break;
			arrName[ncount] =""; //jSondata[ncount].imageName;
			
			arrImage[ncount] = serverDownload +jSondata[ncount].imageIcon + '&w='+ g_nSizeIcon;			
			arrLink[ncount] = '#viewImage';
			arrExtends[ncount] = jSondata[ncount].extendId;
            	
			ncount ++;
		}
		if(ncount > 0)
		{    
    		var extendId = jSondata[0].extendId;
   		    var idContent = "#id" + extendId;		 
    		createGridImage('#category-items',arrLink,arrName,arrImage,arrExtends,g_nInLine,false,true);
        }
		else
		{
            alert("group is empty");
        }
	}
	function showListAudio(jSondata)
	{
	  
	  var arrLink = new Array();
	  var arrName = new Array();	 
	  var arrDate = new Array();
	  var ncount = 0;
	  while(1)
		{
			if(jSondata[ncount] == null)
				break;
			arrName[ncount] = jSondata[ncount].audioName;
			arrLink[ncount] = "#";// +jSondata[ncount].audioName;
			arrDate[ncount] = jSondata[ncount].audioCreate;	
			ncount ++;
		}
		if(ncount > 0)
		{
            
    		var extendId = jSondata[0].extendId;
   		    var idPage = "#id" + extendId;
			//populateList(id,arrLink,arrName,bRefresh) 			
    		populateListWithIndex(idPage,arrLink,arrName,true);
        }
		else
		{
            alert("group is empty");
        }
	}
	function displayGroup(jSondata)
	{
		
	  var arrLink = new Array();
	  var arrName = new Array();
	  var arrImage = new Array();
	  var arrExtends = new Array();
	  var ncount = 0;
	  while(1)
		{
			if(jSondata[ncount] == null)
				break;
			arrName[ncount] = jSondata[ncount].extendName;
			arrImage[ncount] = serverDownload +jSondata[ncount].extendIcon;
			arrLink[ncount] = '#id'+jSondata[ncount].extendId;
			arrExtends[ncount] = jSondata[ncount].extendCommand;
			ncount ++;
		}
		if(ncount > 0)
		{ 
    		var parentId = jSondata[0].parentId;
   		    var idContent = "#content-id" + parentId;		 
    		createGridImageWithouTittle(idContent,arrLink,arrName,arrImage,arrExtends,3,false,true);
        }
		else
            alert("group is empty");
	
	}
	// Show chapter


//End vimic

//WebServices


function checkConnection()
 {
	var networkState = navigator.network.connection.type;

	var states = {};
	states[Connection.UNKNOWN]  = 'Unknown connection';
	states[Connection.ETHERNET] = 'Ethernet connection';
	states[Connection.WIFI]     = 'WiFi connection';
	states[Connection.CELL_2G]  = 'Cell 2G connection';
	states[Connection.CELL_3G]  = 'Cell 3G connection';
	states[Connection.CELL_4G]  = 'Cell 4G connection';
	states[Connection.NONE]     = 'No network connection';		
	if(networkState == Connection.WIFI || 
	networkState == Connection.CELL_3G ||
	networkState == Connection.CELL_4G)
		return true;
	
	else
	{	
		alert('Current connect'+states[networkState]+'you must connect to internet');
		if (navigator.app && navigator.app.exitApp) 
		{
		    navigator.app.exitApp();
		} else if (navigator.device && navigator.device.exitApp) 
		{
		    navigator.device.exitApp();
		}
		return false;
	}
}
	
function connectServer(sServer)
{
	//if(checkConnection()== false)
	//	return null;
	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open("POST", sServer,true);
	//UNSENT (numeric value 0) The object has been constructed.
	//OPENED (numeric value 1) The open() method has been successfully invoked
	//HEADERS_RECEIVED (numeric value 2) 
	//LOADING (numeric value 3)
	//DONE (numeric value 4) 
	//http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-opened
	if(xmlHttp.readyState == 1) //opened
		return xmlHttp;
	else
	{
		xmlHttp.abort();
		return null;
	}		
}	

function getListItems(sMessage,user,ExtendId,callback) 
 {       
	var xmlhttp = connectServer(serverJson);
		if( xmlhttp == null)
			alert("can't connect to server");
			
	xmlhttp.onreadystatechange=function() 
	{	
	 if (xmlhttp.readyState == 4)
		{	
			var sXml ="<?xml version='1.0' encoding='UTF-8'?>";
			var sSoap = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:'+sMessage+'Response xmlns:ns2="http://JsonUtils/"><return>';
			
			var tailRemove = '</return></ns2:'+sMessage+'Response></S:Body></S:Envelope>';
						
			var sReponse = xmlhttp.responseText;			
			sReponse = sReponse.replace(sXml,'');
			sReponse = sReponse.replace(sSoap,'');
			 
			sReponse = sReponse.replace(tailRemove,''); 			
			var JsonString = '{ "objectJson" :' + sReponse + '}';			
			var obj = eval("("+JsonString+")");				
			if(obj)
			{	
				callback(obj.objectJson) ;
			}	
			else
				alert("can't load database");
		}
	}
	xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");  		
	var xml = 
	'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
	   '<soap:Body> ' +
		 '<'+sMessage+' xmlns="http://JsonUtils/"> ' +
		 '<extendName xmlns="" />'+
		  '<prefixCode xmlns="" />'+
		  '<extendId xmlns="">'+ExtendId+'</extendId>'+		  
		  '<username xmlns="">'+user+'</username>'+		  
		  '<status xmlns="">1</status>'+
		  '<minRow xmlns="">0</minRow>'+
	      '<maxRow xmlns="">10</maxRow>'+
		 '</'+sMessage+'> ' +
	   '</soap:Body> ' +
	 '</soap:Envelope>';	 
	xmlhttp.send(xml);   
 } 
 
function getJsonBySql(sSQL,minRow,maxRow,callback) 
{
	var xmlhttp = connectServer(serverJson);
	if( xmlhttp == null)
		alert("can't connect to server");
	 	
	xmlhttp.onreadystatechange=function() 
	{	
	 if (xmlhttp.readyState == 4)
		{	
		 $.mobile.hidePageLoadingMsg();
			var sXml ="<?xml version='1.0' encoding='UTF-8'?>";
			var sSoap = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:'+sMessage+'Response xmlns:ns2="http://JsonUtils/"><return>';
			
			var tailRemove = '</return></ns2:'+sMessage+'Response></S:Body></S:Envelope>';
						
			var sReponse = xmlhttp.responseText;			
			sReponse = sReponse.replace(sXml,'');
			sReponse = sReponse.replace(sSoap,'');
			 
			sReponse = sReponse.replace(tailRemove,''); 			
			var JsonString = '{ "Jsons" :' + sReponse + '}';
			var obj = null;
			if(sReponse != "")
			 obj = eval("("+JsonString+")");				
			if(obj)
			{	
				callback(obj.Jsons) ;
			}	
			else
				alert("can't load database");
		}
	}
	xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");  		
	var xml = 
	'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
	   '<soap:Body> ' +
		 '<'+sMessage+' xmlns="http://JsonUtils/"> ' +		 
		  '<sql xmlns="">'+ sSQL +'</sql>'+		  
		  '<minRow xmlns="">' + minRow + '</minRow>'+
		  'maxRow xmlns="">' + maxRow + '</maxRow>'+
		  '<nativeQuery>true</nativeQuery>'+
		 '</'+sMessage+'> ' +
	   '</soap:Body> ' +
	 '</soap:Envelope>';
	xmlhttp.send(xml); 
}
function getJsonData(sMessage,user,parrentId,callback) 
 {     	
	var xmlhttp = connectServer(serverJson);
		if( xmlhttp == null)
			alert("can't connect to server");
		
	 	
	xmlhttp.onreadystatechange=function() 
	{	
		
	 if (xmlhttp.readyState == 4)
		{			 
		 	//if($("#splash"))
			//{
			//	$("#splash").attr("src", $("#splash").src).fadeIn("slow");
			//}
		 
			var sXml ="<?xml version='1.0' encoding='UTF-8'?>";
			var sSoap = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:'+sMessage+'Response xmlns:ns2="http://JsonUtils/"><return>';
			
			var tailRemove = '</return></ns2:'+sMessage+'Response></S:Body></S:Envelope>';
						
			var sReponse = xmlhttp.responseText;			
			sReponse = sReponse.replace(sXml,'');
			sReponse = sReponse.replace(sSoap,'');
			 
			sReponse = sReponse.replace(tailRemove,''); 			
			var JsonString = '{ "Jsons" :' + sReponse + '}';
			var obj = null;
			if(sReponse != "")
			 obj = eval("("+JsonString+")");				
			if(obj)
			{	
				callback(obj.Jsons) ;
			}	
			else
				alert("can't load database");
		}
	}
	xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");  		
	var xml = 
	'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
	   '<soap:Body> ' +
		 '<'+sMessage+' xmlns="http://JsonUtils/"> ' +
		 '<extendName xmlns="" />'+
		  '<prefixCode xmlns="" />'+
		  '<parentId xmlns="">'+parrentId+'</parentId>'+		  
		  '<username xmlns="">'+user+'</username>'+		  
		  '<status xmlns="">1</status>'+
		 '</'+sMessage+'> ' +
	   '</soap:Body> ' +
	 '</soap:Envelope>';
	xmlhttp.send(xml);   
 } 
//End WebServices